/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.opentoast.maven;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.eclipse.Messages;
import org.apache.maven.plugin.eclipse.writers.wtp.AbstractWtpResourceWriter;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter;

/**
 * Creates a .settings folder and project facet configuration files for SpringSource dm Server.
 * <p>
 * <b>Copy and modified from maven-eclipse-plugin-2.5.1</b>
 * 
 * @author <a href="mailto:jiwhiz@gmail.com">Yuan Ji</a>
 *
 */
public class S2DMFacetsWriter extends AbstractWtpResourceWriter 
{
    /**
     * The .settings folder for config files.
     */
    private static final String DIR_WTP_SETTINGS = ".settings";

    /**
     * File name where Eclipse Project's Facet configuration will be stored.
     */
    private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml";

    private static final String ELT_FACETED_PROJECT = "faceted-project";
    
    private static final String ELT_RUNTIME = "runtime";
    
    private static final String ELT_INSTALLED = "installed";
    
    private static final String ATTR_FACET = "facet";
    
    private static final String ATTR_VERSION = "version";
    
    private static final String ATTR_NAME = "name";
    
    private static final String S2DM_SERVER_NAME = "SpringSource dm Server (Runtime) v1.0";
    
    private static final String FACET_S2DM_BUNDLE = "com.springsource.server.bundle";
    
    private static final String FACET_S2DM_PAR = "com.springsource.server.par";
    
    public void write() throws MojoExecutionException
    {
        // create a .settings directory (if not existing)
        File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
        settingsDir.mkdirs();

        Writer w;
        // Write out facet core xml
        try
        {
            w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" );
        }
        catch ( IOException ex )
        {
            throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
        }
        XMLWriter writer = new PrettyPrintXMLWriter( w );
        if ("dm-par".equals(config.getPackaging())){
            writeS2DMFacetCoreForPar( writer );
        } else{
            writeS2DMFacetCoreForBundle( writer );
        }
        IOUtil.close( w );
    }

    /**
     * Writes out the facet info for a bundle project for S2DM.
     * 
     * @param writer
     */
    private void writeS2DMFacetCoreForBundle( XMLWriter writer)
    {
        writer.startElement( ELT_FACETED_PROJECT );
        
        //add runtime for Spring Source dm Server
       	writer.startElement( ELT_RUNTIME );
       	writer.addAttribute( ATTR_NAME, S2DM_SERVER_NAME );
       	writer.endElement(); // runtime
        
        //add bundle facet
        writer.startElement( ELT_INSTALLED );
        writer.addAttribute( ATTR_FACET, FACET_S2DM_BUNDLE );
        writer.addAttribute( ATTR_VERSION, "1.0" );
        writer.endElement();
        
        writer.endElement(); // faceted-project
    }
    
    /**
     * Writes out the facet info for a par project for S2DM.
     * 
     * @param writer
     */
    private void writeS2DMFacetCoreForPar( XMLWriter writer)
    {
        writer.startElement( ELT_FACETED_PROJECT );
        
        //add runtime for Spring Source dm Server
        writer.startElement( ELT_RUNTIME );
        writer.addAttribute( ATTR_NAME, S2DM_SERVER_NAME );
        writer.endElement(); // runtime
        
       //add par facet
        writer.startElement( ELT_INSTALLED );
        writer.addAttribute( ATTR_FACET, FACET_S2DM_PAR );
        writer.addAttribute( ATTR_VERSION, "1.0" );
        writer.endElement();
        
        writer.endElement(); // faceted-project
    }
}
